<?php
class AdmincpModule extends CWebModule
{
	public function init()
	{
		// this method is called when the module is being created
		// you may place code here to customize the module or the application

		// import the module-level models and components
		$this->setImport(array(
			'application.models.dao.*',
			'admincp.models.*',
			'admincp.components.*',
		));
		
		$this->layoutPath = Yii::getPathOfAlias('admincp.views.layouts');
		$this->layout = 'main';
		
		//define constants
		defined('LINK_ADMINCP') || define('LINK_ADMINCP', BACKEND_URL); //Yii::app()->baseUrl.'/admincp');
		defined('LINK_ADMINCP_POSTS') || define('LINK_ADMINCP_POSTS', LINK_ADMINCP.'/posts');
		defined('LINK_ADMINCP_PAGES') || define('LINK_ADMINCP_PAGES', LINK_ADMINCP.'/posts/pages');
		defined('LINK_ADMINCP_CATEGORIES') || define('LINK_ADMINCP_CATEGORIES', LINK_ADMINCP.'/categories');
		defined('LINK_ADMINCP_MEDIA') || define('LINK_ADMINCP_MEDIA', LINK_ADMINCP.'/media');
		defined('LINK_ADMINCP_TAXONOMIES') || define('LINK_ADMINCP_TAXONOMIES', LINK_ADMINCP.'/taxonomies');		
		
		defined('LINK_ADMINCP_USERS') || define('LINK_ADMINCP_USERS', LINK_ADMINCP.'/users');
		defined('LINK_ADMINCP_ROLES') || define('LINK_ADMINCP_ROLES', LINK_ADMINCP.'/roles');
		defined('LINK_ADMINCP_SUBSCRIPTIONS') || define('LINK_ADMINCP_SUBSCRIPTIONS', LINK_ADMINCP.'/subscriptions');
		defined('LINK_ADMINCP_FEEDBACK') || define('LINK_ADMINCP_FEEDBACK', LINK_ADMINCP.'/feedback');
		
		defined('LINK_ADMINCP_MENU') || define('LINK_ADMINCP_MENU', LINK_ADMINCP.'/menu');
		defined('LINK_ADMINCP_EMAILTEMPLATES') || define('LINK_ADMINCP_EMAILTEMPLATES', LINK_ADMINCP.'/emailtemplates');
		defined('LINK_ADMINCP_ADVERTISEMENTS') || define('LINK_ADMINCP_ADVERTISEMENTS', LINK_ADMINCP.'/advertisements');
		defined('LINK_ADMINCP_LANGUAGES') || define('LINK_ADMINCP_LANGUAGES', LINK_ADMINCP.'/languages');
		
		defined('LINK_ADMINCP_CUSTOMCONFIGS') || define('LINK_ADMINCP_CUSTOMCONFIGS', LINK_ADMINCP.'/configs');
		defined('LINK_ADMINCP_SYSTEM') || define('LINK_ADMINCP_SYSTEM', LINK_ADMINCP.'/index/info');
		defined('LINK_ADMINCP_NOTFOUND') || define('LINK_ADMINCP_NOTFOUND', LINK_ADMINCP.'/error/404');

	}

	public function beforeControllerAction($controller, $action)
	{
		if(parent::beforeControllerAction($controller, $action))
		{
			// this method is called before any module controller action is performed
			// you may place customized code here
			//$strModuleName = Yii::app()->controller->module->id;
			//$strControllerName = Yii::app()->controller->id;
			//$strActionName = Yii::app()->controller->action->id ;

			if (Yii::app()->admin->isGuest && !($controller->id=='auth' && ($action->id=='login' || $action->id=='captcha' || $action->id=='reset'))) {
				Yii::app()->request->redirect(Yii::app()->admin->loginUrl);
			}
			else if(!Yii::app()->admin->isGuest && $controller->id=='auth' && $action->id=='login'){
				Yii::app()->request->redirect(Yii::app()->admin->returnUrl);
			}
			
			return true;
		}
		else
			return false;
	}
}
